library(caret)
library(lme4)
library(lmerTest)
library(ggplot2)
library(stringi)
library(gridExtra)
library(ggfortify)
library(dendextend)
library(psych)
library(kableExtra)
library(tidyverse)
library(dtplyr)
source('other_functions.R')
source('plotting_functions.R')
data.list <- readRDS(params$input_data_p)
dat.l <- data.list$dat.l # data in long format
dat.w <- data.list$dat.w # data in wide format
# which proteins were spiked in?
spiked.proteins <- dat.l %>% distinct(Protein) %>% filter(stri_detect(Protein, fixed='ups')) %>% pull %>% as.character
tmp=dat.l %>% distinct(Protein) %>% pull %>% as.character
# protein subsampling
if (params$subsample_p>0 & params$subsample_p==floor(params$subsample_p) & params$subsample_p<=length(tmp)){
sub.prot <- tmp[sample(1:length(tmp), size=params$subsample_p)]
if (length(spiked.proteins)>0) sub.prot <- c(sub.prot,spiked.proteins)
dat.l <- dat.l %>% filter(Protein %in% sub.prot)
dat.w <- dat.w %>% filter(Protein %in% sub.prot)
}
# specify # of varying component variants and their names
variant.names <- c('log2_intensity', 'intensity', 'ratio_rowmean', 'ratio_condmean', 'ratio_condpairs', 'ratio_onetag')
n.comp.variants <- length(variant.names)
scale.vec <- c('log', 'raw','raw','raw', 'raw','raw') # ratios are considered raw, because they are basically mean-normalized intensities
# get some data parameters created in the data_prep script
referenceCondition <- data.list$data.params$referenceCondition
channelsOrdered <- data.list$data.params$channelsOrdered
condition.color <- data.list$data.params$condition.color
ma.onesample.num <- data.list$data.params$ma.onesample.num
ma.onesample.denom <- data.list$data.params$ma.onesample.denom
ma.allsamples.num <- data.list$data.params$ma.allsamples.num
ma.allsamples.denom <- data.list$data.params$ma.allsamples.denom
# create data frame with sample information
sample.info <- get_sample_info(dat.l, condition.color)
# get channel names
channelNames <- remove_factors(unique(sample.info$Channel))
dat.unit.l <- emptyList(variant.names)
dat.unit.l$log2_intensity <- dat.l %>% mutate(response=log2(intensity)) %>% select(-intensity)
dat.unit.l$intensity <- dat.l %>% rename(response=intensity)
ratio_rowmean: Calculate ratio (per PSM) with respect to average intensity within run, or in other words: each value is divided by the row mean.
# use half-wide data to compute within-run average of PSM channels corresponding to the reference Condition
refCols <- sample.info %>% distinct(Channel) %>% pull(Channel)
denom.df=dat.l %>% pivot_wider(id_cols=-one_of('Condition', 'BioReplicate'),names_from='Channel', values_from='intensity')
denom.df$denom=apply(denom.df[,refCols], 1, function(x) mean(x, na.rm=T))
denom.df=denom.df[,c('Run', 'Protein', 'Peptide', 'RT', 'Charge', 'PTM', 'denom')]
dat.unit.l$ratio_rowmean <- dat.l %>% left_join(denom.df, by=c('Run', 'Protein', 'Peptide', 'RT', 'Charge', 'PTM')) %>% mutate(response=intensity/denom) %>% select(-c(intensity, denom))
ratio_condmean: each sample is divided by the average of samples belonging to the reference condition (here ‘r referenceCondition’)
# use half-wide data to compute within-run average of PSM channels corresponding to the reference Condition
refCols <- sample.info %>% filter(Condition==referenceCondition) %>% distinct(Channel) %>% pull
denom.df=dat.l %>% filter(Condition==referenceCondition) %>% pivot_wider(id_cols=-one_of('Condition', 'BioReplicate'),names_from='Channel', values_from='intensity')
denom.df$denom=apply(denom.df[,refCols], 1, function(x) mean(x, na.rm=T))
denom.df=denom.df[,c('Run', 'Protein', 'Peptide', 'RT', 'Charge', 'PTM', 'denom')]
dat.unit.l$ratio_condmean <- dat.l %>% left_join(denom.df, by=c('Run', 'Protein', 'Peptide', 'RT', 'Charge', 'PTM')) %>% mutate(response=intensity/denom) %>% select(-c(intensity, denom))
ratio_condpairs: compute ratios using samples from a selected condition as ratio denominator (channels paired from left to right)
dat.unit.l$ratio_condpairs <- compute_ratio(dat.w, sample.info, channelsOrdered, referenceCondition)
ratio_onetag: divide by one channel reserved for the reference condition (here the first channel of 0.5)
refCols <- sample.info %>% filter(Condition %in% referenceCondition) %>% group_by(Run) %>% top_n(1, Channel) %>% pull(Sample) %>% as.character()
left_data=dat.unit.l$intensity %>% filter(paste(Run, Channel, sep=':') %in% refCols) %>% select('Run', 'Protein', 'Peptide', 'RT', 'Charge', 'PTM', 'response') %>% rename(denom=response)
dat.unit.l$ratio_onetag=left_join(dat.unit.l$intensity, left_data, by=c('Run', 'Protein', 'Peptide', 'RT', 'Charge', 'PTM'))
dat.unit.l$ratio_onetag <- dat.unit.l$ratio_onetag %>% mutate(response=response/denom) %>% select(-denom)
# no summarization
dat.summ.l <- dat.unit.l
dat.norm.l <- dat.summ.l
# fit normalization model
norm.models <- lapply(dat.summ.l, function(x) return(lmer(response ~ Run + Run:Channel + (1|Protein) + (1|Run:Peptide), data=x)))
# assign normalized values
for (i in 1:n.comp.variants){
# add the normalization model intercept only when on log scale
if (variant.names[i]=='log2_intensity') {dat.norm.l[[variant.names[i]]]$response <- residuals(norm.models[[variant.names[i]]]) + fixef(norm.models[[variant.names[i]]])['(Intercept)']} else { dat.norm.l[[variant.names[i]]]$response <- residuals(norm.models[[variant.names[i]]])}}
# # apply the 'fix' - add the model intercept to the residuals
# dat.norm.l$intensity_fix <- dat.norm.l$intensity; dat.norm.l$intensity_fix$response <- dat.norm.l$intensity_fix$response + fixef(norm.models$intensity)['(Intercept)']
# dat.norm.l$ratio_fix <- dat.norm.l$ratio; dat.norm.l$ratio_fix$response <- dat.norm.l$ratio_fix$response + fixef(norm.models$ratio)['(Intercept)']
# # update variant names, #, scale
# variant.names <- names(dat.norm.l)
# scale.vec <- c(scale.vec, 'raw', 'raw')
# n.comp.variants <- length(variant.names)
# tmp <- c("log2_intensity", "intensity", "intensity_fix", "ratio", "ratio_fix")
# variant.names <- tmp
# dat.norm.l <- dat.norm.l[tmp]
# # for technical reasons, assign new variants to dat.summ.l
# dat.summ.l$intensity_fix <- dat.summ.l$intensity
# dat.summ.l$ratio_fix <- dat.summ.l$ratio
# dat.summ.l <- dat.summ.l[tmp]
dat.nonnorm.summ.l <- lapply(dat.summ.l, function(x) aggFunc(x, 'response', group.vars=c('Mixture', 'TechRepMixture', 'Run', 'Channel', 'Condition', 'BioReplicate', 'Protein', 'Peptide'), 'median'))
dat.nonnorm.summ.l <- lapply(dat.nonnorm.summ.l, function(x) aggFunc(x, 'response', group.vars=c('Mixture', 'TechRepMixture', 'Run', 'Channel', 'Condition', 'BioReplicate', 'Protein'), 'median'))
dat.norm.summ.l <- lapply(dat.norm.l, function(x) aggFunc(x, 'response', group.vars=c('Mixture', 'TechRepMixture', 'Run', 'Channel', 'Condition', 'BioReplicate', 'Protein', 'Peptide'), 'median'))
dat.norm.summ.l <- lapply(dat.norm.summ.l, function(x) aggFunc(x, 'response', group.vars=c('Mixture', 'TechRepMixture', 'Run', 'Channel', 'Condition', 'BioReplicate', 'Protein'), 'median'))
# make data completely wide (also across runs)
## normalized data
dat.norm.summ.w2 <- lapply(dat.norm.summ.l, function(x){
dat.tmp <- pivot_wider(data=x, id_cols=Protein, names_from=Run:Channel, values_from=response, names_sep=':')
return(dat.tmp)})
## non-normalized data
dat.nonnorm.summ.w2 <- lapply(dat.nonnorm.summ.l, function(x){
dat.tmp <- pivot_wider(data=x, id_cols=Protein, names_from=Run:Channel, values_from=response, names_sep=':')
return(dat.tmp)})
par(mfrow=c(1,2))
for (i in 1:n.comp.variants){
boxplot_ils(dat.nonnorm.summ.l[[variant.names[i]]], paste('raw', variant.names[i], sep='_'))
boxplot_ils(dat.norm.summ.l[[variant.names[i]]], paste('normalized', variant.names[i], sep='_'))}
MA plots of two single samples taken from condition 1 and condition 0.125, measured in different MS runs (samples Mixture2_1:127C and Mixture1_2:129N, respectively).
for (i in 1:n.comp.variants){
p1 <- maplot_ils(dat.nonnorm.summ.w2[[variant.names[i]]], ma.onesample.num, ma.onesample.denom, scale.vec[i], paste('raw', variant.names[i], sep='_'), spiked.proteins)
p2 <- maplot_ils(dat.norm.summ.w2[[variant.names[i]]], ma.onesample.num, ma.onesample.denom, scale.vec[i], paste('normalized', variant.names[i], sep='_'), spiked.proteins)
grid.arrange(p1, p2, ncol=2)}
MA plots of all samples from condition 1 and condition 0.125 (quantification values averaged within condition).
channels.num <- sample.info %>% filter(Condition==ma.allsamples.num) %>% distinct(Run:Channel) %>% pull
channels.denom <- sample.info %>% filter(Condition==ma.allsamples.denom) %>% distinct(Run:Channel) %>% pull
for (i in 1:n.comp.variants){
p1 <- maplot_ils(dat.nonnorm.summ.w2[[variant.names[i]]], channels.num, channels.denom, scale=scale.vec[i], paste('raw', variant.names[i], sep='_'), spiked.proteins)
p2 <- maplot_ils(dat.norm.summ.w2[[variant.names[i]]], channels.num, channels.denom, scale=scale.vec[i], paste('normalized', variant.names[i], sep='_'), spiked.proteins)
grid.arrange(p1, p2, ncol=2)}
par(mfrow=c(1, 2))
for (i in 1:n.comp.variants){
pca.scale=FALSE
if (variant.names[i] %in% c('intensity', 'intensity_fix')) pca.scale=TRUE
pcaplot_ils(dat.nonnorm.summ.w2[[variant.names[i]]] %>% select(-'Protein'), info=sample.info, paste('raw', variant.names[i], sep='_'), scale=pca.scale)
pcaplot_ils(dat.norm.summ.w2[[variant.names[i]]] %>% select(-'Protein'), info=sample.info, paste('normalized', variant.names[i], sep='_'), scale=pca.scale)}
par(mfrow=c(1, 2))
for (i in 1:n.comp.variants){
pca.scale=FALSE
if (variant.names[i] %in% c('intensity', 'intensity_fix')) pca.scale=TRUE
pcaplot_ils(dat.nonnorm.summ.w2[[variant.names[i]]] %>% filter(Protein %in% spiked.proteins) %>% select(-'Protein'), info=sample.info, paste('raw', variant.names[i], sep='_'), scale=pca.scale)
pcaplot_ils(dat.norm.summ.w2[[variant.names[i]]] %>% filter(Protein %in% spiked.proteins) %>% select(-'Protein'), info=sample.info, paste('normalized', variant.names[i], sep='_'), scale=pca.scale)}
HC (hierarchical clustering) plots
par(mfrow=c(1,2))
for (i in 1:n.comp.variants){
dendrogram_ils(dat.nonnorm.summ.w2[[variant.names[i]]] %>% select(-'Protein'), info=sample.info, paste('raw', variant.names[i], sep='_'))
dendrogram_ils(dat.norm.summ.w2[[variant.names[i]]] %>% select(-'Protein'), info=sample.info, paste('normalized', variant.names[i], sep='_'))}
Below the histograms of p-values from the linear model explaining the response variable with Run as a covariate.
plots <- vector('list', n.comp.variants)
for (i in 1:n.comp.variants){
dat <- list(dat.nonnorm.summ.l[[variant.names[i]]], dat.norm.summ.l[[variant.names[i]]])
names(dat) <- c(paste('raw', variant.names[i], sep='_'), paste('normalized', variant.names[i], sep='_'))
plots[[i]] <- run_effect_plot(dat)}
grid.arrange(grobs = plots, nrow=n.comp.variants)
Intra-protein correlation modelled with 1|Run:Channel random effect.
dat.dea <- emptyList(variant.names)
for(i in seq_along(dat.dea)){
dat.dea[[variant.names[i]]] <- lmm_dea(dat=dat.norm.l[[variant.names[i]]], mod.formula='response ~ Condition + (1|Run:Channel)', scale=scale.vec[i], referenceCondition)}
# character vectors containing logFC and p-values columns
dea.cols <- colnames(dat.dea[[1]])
logFC.cols <- dea.cols[stri_detect_fixed(dea.cols, 'logFC')]
significance.cols <- dea.cols[stri_detect_fixed(dea.cols, 'q.mod')]
n.contrasts <- length(logFC.cols)
cm <- conf_mat(dat.dea, 'q.mod', 0.05, spiked.proteins)
print_conf_mat(cm, referenceCondition)
| background | spiked | background | spiked | background | spiked | background | spiked | background | spiked | background | spiked | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| not_DE | 4061 | 4 | 4062 | 18 | 4061 | 9 | 4062 | 12 | 4062 | 16 | 4063 | 15 |
| DE | 3 | 15 | 0 | 1 | 2 | 10 | 1 | 7 | 2 | 3 | 1 | 4 |
| log2_intensity | intensity | ratio_rowmean | ratio_condmean | ratio_condpairs | ratio_onetag | |
|---|---|---|---|---|---|---|
| Accuracy | 0.998 | 0.996 | 0.997 | 0.997 | 0.996 | 0.996 |
| Sensitivity | 0.789 | 0.053 | 0.526 | 0.368 | 0.158 | 0.211 |
| Specificity | 0.999 | 1.000 | 1.000 | 1.000 | 1.000 | 1.000 |
| PPV | 0.833 | 1.000 | 0.833 | 0.875 | 0.600 | 0.800 |
| NPV | 0.999 | 0.996 | 0.998 | 0.997 | 0.996 | 0.996 |
| background | spiked | background | spiked | background | spiked | background | spiked | background | spiked | background | spiked | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| not_DE | 4064 | 17 | 4062 | 19 | 4063 | 18 | 4063 | 19 | 4064 | 19 | 4064 | 19 |
| DE | 0 | 2 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| log2_intensity | intensity | ratio_rowmean | ratio_condmean | ratio_condpairs | ratio_onetag | |
|---|---|---|---|---|---|---|
| Accuracy | 0.996 | 0.995 | 0.996 | 0.995 | 0.995 | 0.995 |
| Sensitivity | 0.105 | 0.000 | 0.053 | 0.000 | 0.000 | 0.000 |
| Specificity | 1.000 | 1.000 | 1.000 | 1.000 | 1.000 | 1.000 |
| PPV | 1.000 | NaN | 1.000 | NaN | NaN | NaN |
| NPV | 0.996 | 0.995 | 0.996 | 0.995 | 0.995 | 0.995 |
| background | spiked | background | spiked | background | spiked | background | spiked | background | spiked | background | spiked | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| not_DE | 4061 | 4 | 4061 | 14 | 4061 | 3 | 4062 | 7 | 4060 | 9 | 4063 | 8 |
| DE | 3 | 15 | 1 | 5 | 2 | 16 | 1 | 12 | 4 | 10 | 1 | 11 |
| log2_intensity | intensity | ratio_rowmean | ratio_condmean | ratio_condpairs | ratio_onetag | |
|---|---|---|---|---|---|---|
| Accuracy | 0.998 | 0.996 | 0.999 | 0.998 | 0.997 | 0.998 |
| Sensitivity | 0.789 | 0.263 | 0.842 | 0.632 | 0.526 | 0.579 |
| Specificity | 0.999 | 1.000 | 1.000 | 1.000 | 0.999 | 1.000 |
| PPV | 0.833 | 0.833 | 0.889 | 0.923 | 0.714 | 0.917 |
| NPV | 0.999 | 0.997 | 0.999 | 0.998 | 0.998 | 0.998 |
scatterplot_ils(dat.dea, significance.cols, 'q-values', spiked.proteins, referenceCondition)
scatterplot_ils(dat.dea, logFC.cols, 'log2FC', spiked.proteins, referenceCondition)
for (i in 1:n.contrasts){
volcanoplot_ils(dat.dea, i, spiked.proteins, referenceCondition)}
Let’s see whether the spiked protein fold changes make sense
# plot theoretical value (horizontal lines) and violin per variant
violinplot_ils(lapply(dat.dea, function(x) x[spiked.proteins, logFC.cols]), referenceCondition)
sessionInfo()
## R version 4.0.3 (2020-10-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252
## [3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
## [5] LC_TIME=English_United Kingdom.1252
##
## attached base packages:
## [1] stats4 parallel stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] dtplyr_1.0.1 forcats_0.5.0 stringr_1.4.0 dplyr_1.0.2 purrr_0.3.4
## [6] readr_1.4.0 tidyr_1.1.2 tibble_3.0.4 tidyverse_1.3.0 kableExtra_1.3.1
## [11] psych_2.0.9 dendextend_1.14.0 ggfortify_0.4.11 gridExtra_2.3 stringi_1.5.3
## [16] lmerTest_3.1-3 lme4_1.1-25 Matrix_1.2-18 caret_6.0-86 ggplot2_3.3.2
## [21] lattice_0.20-41 rmarkdown_2.5 pacman_0.5.1
##
## loaded via a namespace (and not attached):
## [1] ModelMetrics_1.2.2.2 R.methodsS3_1.8.1 knitr_1.30 multcomp_1.4-14
## [5] R.utils_2.10.1 data.table_1.13.2 rpart_4.1-15 doParallel_1.0.16
## [9] generics_0.1.0 BiocGenerics_0.34.0 preprocessCore_1.50.0 TH.data_1.0-10
## [13] webshot_0.5.2 xml2_1.3.2 lubridate_1.7.9 assertthat_0.2.1
## [17] viridis_0.5.1 gower_0.2.2 xfun_0.19 hms_0.5.3
## [21] evaluate_0.14 fansi_0.4.1 dbplyr_2.0.0 readxl_1.3.1
## [25] DBI_1.1.0 tmvnsim_1.0-2 ellipsis_0.3.1 backports_1.1.10
## [29] signal_0.7-6 libcoin_1.0-6 vctrs_0.3.4 Biobase_2.48.0
## [33] withr_2.3.0 mnormt_2.0.2 crayon_1.3.4 recipes_0.1.15
## [37] pkgconfig_2.0.3 labeling_0.4.2 nlme_3.1-149 ProtGenerics_1.20.0
## [41] nnet_7.3-14 rlang_0.4.8 lifecycle_0.2.0 sandwich_3.0-0
## [45] affyio_1.58.0 modelr_0.1.8 cellranger_1.1.0 matrixStats_0.57.0
## [49] boot_1.3-25 zoo_1.8-8 reprex_0.3.0 png_0.1-7
## [53] viridisLite_0.3.0 R.oo_1.24.0 pROC_1.16.2 S4Vectors_0.26.1
## [57] scales_1.1.1 magrittr_1.5 plyr_1.8.6 zlibbioc_1.34.0
## [61] compiler_4.0.3 pcaMethods_1.80.0 cli_2.1.0 affy_1.66.0
## [65] MASS_7.3-53 mgcv_1.8-33 tidyselect_1.1.0 vsn_3.56.0
## [69] highr_0.8 yaml_2.2.1 MALDIquant_1.19.3 grid_4.0.3
## [73] tools_4.0.3 rstudioapi_0.12 foreach_1.5.1 prodlim_2019.11.13
## [77] farver_2.0.3 mzID_1.26.0 digest_0.6.26 BiocManager_1.30.10
## [81] lava_1.6.8.1 Rcpp_1.0.5 broom_0.7.2 ncdf4_1.17
## [85] httr_1.4.2 colorspace_1.4-1 rvest_0.3.6 XML_3.99-0.5
## [89] fs_1.5.0 IRanges_2.22.2 splines_4.0.3 statmod_1.4.35
## [93] jsonlite_1.7.1 nloptr_1.2.2.2 timeDate_3043.102 modeltools_0.2-23
## [97] ipred_0.9-9 R6_2.5.0 pillar_1.4.6 htmltools_0.5.0
## [101] glue_1.4.2 minqa_1.2.4 BiocParallel_1.22.0 class_7.3-17
## [105] codetools_0.2-16 mvtnorm_1.1-1 utf8_1.1.4 numDeriv_2016.8-1.1
## [109] survival_3.2-7 limma_3.44.3 admisc_0.11 munsell_0.5.0
## [113] e1071_1.7-4 iterators_1.0.13 impute_1.62.0 haven_2.3.1
## [117] reshape2_1.4.4 gtable_0.3.0